package com.zb.tools;

import okhttp3.*;
import okio.BufferedSink;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class OkHttpKit {

    private OkHttpClient client;

    public String get(String url)
    {
        Request request=new Request.Builder()
            .url(url).build();
        try
        {

            Response response=client.newCall(request).execute();
            return response.body().string();
        }
        catch (IOException e)
        {
            throw new RuntimeException("get fail:"+e.getMessage());
        }
    }

    public String get(String url, Map<String,String> headers)
    {
        Request.Builder builder=new Request.Builder();
        builder.url(url);
        for(Map.Entry<String,String> kv : headers.entrySet())
            builder.addHeader(kv.getKey(),kv.getValue());
        try
        {

            Response response=client.newCall(builder.build()).execute();
            return response.body().string();
        }
        catch (IOException e)
        {
            throw new RuntimeException("get fail:"+e.getMessage());
        }
    }

    public String post(String url, String str, Map<String,String> headers)
    {
        Request.Builder builder=new Request.Builder();
        builder.url(url);
        builder.post(RequestBody.create(str,MediaType.get("text/plain; charset=utf-8")));
        if(headers!=null)
            for(Map.Entry<String,String> kv : headers.entrySet())
                builder.addHeader(kv.getKey(),kv.getValue());
        try
        {

            Response response=client.newCall(builder.build()).execute();
            return response.body().string();
        }
        catch (IOException e)
        {
            throw new RuntimeException("get fail:"+e.getMessage());
        }
    }

    public String postForm(String url, Map<String,String> params, Map<String,String> headers)
    {
        Request.Builder builder=new Request.Builder();
        builder.url(url);
        FormBody.Builder fb = new FormBody.Builder();
        if(params!=null)
            for(Map.Entry<String,String> kv : params.entrySet())
                fb.add(kv.getKey(),kv.getValue());
        builder.post(fb.build());
        if(headers!=null)
            for(Map.Entry<String,String> kv : headers.entrySet())
                builder.addHeader(kv.getKey(),kv.getValue());
        try
        {

            Response response=client.newCall(builder.build()).execute();
            return response.body().string();
        }
        catch (IOException e)
        {
            throw new RuntimeException("get fail:"+e.getMessage());
        }
    }

    public String postJson(String url, String json, Map<String,String> headers)
    {
        Request.Builder builder=new Request.Builder();
        builder.url(url);
        if(json==null) json="";
        builder.post(RequestBody.create(json,MediaType.get("application/json; charset=utf-8")));
        if(headers!=null)
        for(Map.Entry<String,String> kv : headers.entrySet())
            builder.addHeader(kv.getKey(),kv.getValue());
        try
        {

            Response response=client.newCall(builder.build()).execute();
            return response.body().string();
        }
        catch (IOException e)
        {
            throw new RuntimeException("get fail:"+e.getMessage());
        }
    }

    private OkHttpKit()
    {
        client=new OkHttpClient();
    }

    //===================================================

    public static OkHttpKit create()
    {
        return new OkHttpKit();
    }
}
